Imports System.Data.SqlClient
Imports System.Drawing
Imports DevExpress.XtraEditors
'--- Created Date On : 07-Oct-2010 (Jagdish)

Public Class frmReviewOfSystem
    Private VisitId As Integer
    Private IsSaved As Boolean = False
    Dim mblnchanged As Boolean = False

    Public Sub New(ByVal id As Integer)
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call
        VisitId = id

    End Sub

    Private Sub frmReviewOfSystem_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        'Ask user to save changes.
        'If IsSaved = False And btnSave.Enabled = True Then
        '    If MessageBox.Show("Do you want to save changes?", Application.ProductName, MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes Then
        '        Call btnSave_Click(sender, e)
        '    End If
        'End If
        If mblnchanged = True And IsSaved = False Then
            If MessageBox.Show("Do you want to save changes?", Application.ProductName, MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes Then
                Call btnSave_Click(sender, e)
            End If
        End If
    End Sub

    Private Sub frmReviewOfSystem_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        PopulateCheckListBoxes()
        FillCheckListBoxes()
        mblnchanged = False
    End Sub

    Private Sub FillCheckListBoxes()
        Dim i, j As Integer

        'fill lstROSRS
        Dim dsTemp As DataSet
        dsTemp = FillDataSet("SELECT RosTable, ROSId  FROM tblVisitROS Where VisitId=" & VisitId & " and RosTable='LtblROSRS'")
        If dsTemp.Tables(0).Rows.Count > 0 Then
            For i = 0 To dsTemp.Tables(0).Rows.Count - 1
                For j = 0 To lstROSRS.ItemCount - 1
                    If lstROSRS.GetItemValue(j) = dsTemp.Tables(0).Rows(i).Item("ROSId") Then
                        lstROSRS.SetItemChecked(j, True)
                    End If
                Next
            Next
        End If
        dsTemp.Dispose()

        'fill lstROSCVS
        dsTemp = FillDataSet("SELECT RosTable, ROSId  FROM tblVisitROS Where VisitId=" & VisitId & " and RosTable='LtblROSCVS'")
        If dsTemp.Tables(0).Rows.Count > 0 Then
            For i = 0 To dsTemp.Tables(0).Rows.Count - 1
                For j = 0 To lstROSCVS.ItemCount - 1
                    If lstROSCVS.GetItemValue(j) = dsTemp.Tables(0).Rows(i).Item("ROSId") Then
                        lstROSCVS.SetItemChecked(j, True)
                    End If
                Next
            Next
        End If
        dsTemp.Dispose()

        'fill lstROSGU
        dsTemp = FillDataSet("SELECT RosTable, ROSId  FROM tblVisitROS Where VisitId=" & VisitId & " and RosTable='LtblROSGU'")
        If dsTemp.Tables(0).Rows.Count > 0 Then
            For i = 0 To dsTemp.Tables(0).Rows.Count - 1
                For j = 0 To lstROSGU.ItemCount - 1
                    If lstROSGU.GetItemValue(j) = dsTemp.Tables(0).Rows(i).Item("ROSId") Then
                        lstROSGU.SetItemChecked(j, True)
                    End If
                Next
            Next
        End If
        dsTemp.Dispose()

        'fill lstROSGI
        dsTemp = FillDataSet("SELECT RosTable, ROSId  FROM tblVisitROS Where VisitId=" & VisitId & " and RosTable='LtblROSGI'")
        If dsTemp.Tables(0).Rows.Count > 0 Then
            For i = 0 To dsTemp.Tables(0).Rows.Count - 1
                For j = 0 To lstROSGI.ItemCount - 1
                    If lstROSGI.GetItemValue(j) = dsTemp.Tables(0).Rows(i).Item("ROSId") Then
                        lstROSGI.SetItemChecked(j, True)
                    End If
                Next
            Next
        End If
        dsTemp.Dispose()

        'fill lstROSCNS
        dsTemp = FillDataSet("SELECT RosTable, ROSId  FROM tblVisitROS Where VisitId=" & VisitId & " and RosTable='LtblROSCNS'")
        If dsTemp.Tables(0).Rows.Count > 0 Then
            For i = 0 To dsTemp.Tables(0).Rows.Count - 1
                For j = 0 To lstROSCNS.ItemCount - 1
                    If lstROSCNS.GetItemValue(j) = dsTemp.Tables(0).Rows(i).Item("ROSId") Then
                        lstROSCNS.SetItemChecked(j, True)
                    End If
                Next
            Next
        End If
        dsTemp.Dispose()
    End Sub

    Private Sub PopulateCheckListBoxes()

        'Populate Checked List Boxes
        Call PopulateCheckedListboxDX(lstROSGI, "Text", "ROSID", "LtblROSGI", "ROSID,Text", , "SortOrder")
        Call PopulateCheckedListboxDX(lstROSGU, "Text", "ROSID", "LtblROSGU", "ROSID,Text", , "SortOrder")
        Call PopulateCheckedListboxDX(lstROSRS, "Text", "ROSID", "LtblROSRS", "ROSID,Text", , "SortOrder")
        Call PopulateCheckedListboxDX(lstROSCVS, "Text", "ROSID", "LtblROSCVS", "ROSID,Text", , "SortOrder")
        Call PopulateCheckedListboxDX(lstROSCNS, "Text", "ROSID", "LtblROSCNS", "ROSID,Text", , "SortOrder")
    End Sub


    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Dim blnIsTransactionOpen As Boolean
        Dim strQuery As String
        Dim intCtr As Integer
        Try
            gtrnWebPlusMed = gconWebPlusMed.BeginTransaction
            blnIsTransactionOpen = True
            'Delete all the rows for that visit
            Call DeleteFromTable("tblVisitROS", "VisitId=" & VisitId)
            'LtblROSGI
            For intCtr = 0 To lstROSGI.ItemCount - 1
                If lstROSGI.GetItemChecked(intCtr) = True Then
                    strQuery = lstROSGI.GetItemValue(intCtr) & _
                                ",'LtblROSGI'" & _
                                "," & VisitId
                    Call InsertintoTable("tblVisitROS", strQuery)
                End If
            Next
            'LtblROSGU
            For intCtr = 0 To lstROSGU.ItemCount - 1
                If lstROSGU.GetItemChecked(intCtr) = True Then
                    strQuery = lstROSGU.GetItemValue(intCtr) & _
                                ",'LtblROSGU'" & _
                                "," & VisitId
                    Call InsertintoTable("tblVisitROS", strQuery)
                End If
            Next
            'LtblROSCNS
            For intCtr = 0 To lstROSCNS.ItemCount - 1
                If lstROSCNS.GetItemChecked(intCtr) = True Then
                    strQuery = lstROSCNS.GetItemValue(intCtr) & _
                                ",'LtblROSCNS'" & _
                                "," & VisitId
                    Call InsertintoTable("tblVisitROS", strQuery)
                End If
            Next
            'LtblROSRS
            For intCtr = 0 To lstROSRS.ItemCount - 1
                If lstROSRS.GetItemChecked(intCtr) = True Then
                    strQuery = lstROSRS.GetItemValue(intCtr) & _
                                ",'LtblROSRS'" & _
                                "," & VisitId
                    Call InsertintoTable("tblVisitROS", strQuery)
                End If
            Next
            'LtblROSCVS
            For intCtr = 0 To lstROSCVS.ItemCount - 1
                If lstROSCVS.GetItemChecked(intCtr) = True Then
                    strQuery = lstROSCVS.GetItemValue(intCtr) & _
                                ",'LtblROSCVS'" & _
                                "," & VisitId
                    Call InsertintoTable("tblVisitROS", strQuery)
                End If
            Next

            gtrnWebPlusMed.Commit()
            blnIsTransactionOpen = False

            IsSaved = True
            Me.Close()
        Catch ex As Exception
            If blnIsTransactionOpen = True Then
                gtrnWebPlusMed.Rollback()
            End If
            MessageBox.Show(ex.Message, gstrMessage, MessageBoxButtons.OK, MessageBoxIcon.Error)
            AddErrorLog("Error " & Err.Number & " (" & Err.Description & ") in Procedure " & _
                Reflection.MethodBase.GetCurrentMethod.Name & " Of " & Me.Name)
        End Try
    End Sub

    Private Sub lstROSGI_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstROSGI.DoubleClick
        With frmToolsLookUp
            .mblnFrmOtherForm = True
            .mstrLookUpValue = "LtblROSGI"
            .mintGrbId = 2
            .ShowInTaskbar = False
            .ShowDialog()
            Call PopulateCheckedListboxDX(lstROSGI, "Text", "ROSID", "LtblROSGI", "ROSID,Text", , "SortOrder")
            .Dispose()
            GC.Collect()
        End With
    End Sub

    Private Sub lstROSGU_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstROSGU.DoubleClick
        With frmToolsLookUp
            .mblnFrmOtherForm = True
            .mstrLookUpValue = "LtblROSGU"
            .mintGrbId = 2
            .ShowInTaskbar = False
            .ShowDialog()
            Call PopulateCheckedListboxDX(lstROSGU, "Text", "ROSID", "LtblROSGU", "ROSID,Text", , "SortOrder")
            .Dispose()
            GC.Collect()
        End With

    End Sub

    Private Sub lstROSRS_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstROSRS.DoubleClick
        With frmToolsLookUp
            .mblnFrmOtherForm = True
            .mstrLookUpValue = "LtblROSRS"
            .mintGrbId = 2
            .ShowInTaskbar = False
            .ShowDialog()
            Call PopulateCheckedListboxDX(lstROSRS, "Text", "ROSID", "LtblROSRS", "ROSID,Text", , "SortOrder")
            .Dispose()
            GC.Collect()
        End With
    End Sub

    Private Sub lstROSCVS_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstROSCVS.DoubleClick
        With frmToolsLookUp
            .mblnFrmOtherForm = True
            .mstrLookUpValue = "LtblROSCVS"
            .mintGrbId = 2
            .ShowInTaskbar = False
            .ShowDialog()
            Call PopulateCheckedListboxDX(lstROSCVS, "Text", "ROSID", "LtblROSCVS", "ROSID,Text", , "SortOrder")
            .Dispose()
            GC.Collect()
        End With
    End Sub

    Private Sub lstROSCNS_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstROSCNS.DoubleClick
        With frmToolsLookUp
            .mblnFrmOtherForm = True
            .mstrLookUpValue = "LtblROSCNS"
            .mintGrbId = 2
            .ShowInTaskbar = False
            .ShowDialog()
            Call PopulateCheckedListboxDX(lstROSCNS, "Text", "ROSID", "LtblROSCNS", "ROSID,Text", , "SortOrder")
            .Dispose()
            GC.Collect()
        End With

    End Sub

    Private Sub lstROSGI_ItemCheck(ByVal sender As System.Object, ByVal e As DevExpress.XtraEditors.Controls.ItemCheckEventArgs) Handles lstROSGI.ItemCheck
        mblnchanged = True
    End Sub

    Private Sub lstROSGU_ItemCheck(ByVal sender As System.Object, ByVal e As DevExpress.XtraEditors.Controls.ItemCheckEventArgs) Handles lstROSGU.ItemCheck
        mblnchanged = True
    End Sub

    Private Sub lstROSRS_ItemCheck(ByVal sender As System.Object, ByVal e As DevExpress.XtraEditors.Controls.ItemCheckEventArgs) Handles lstROSRS.ItemCheck
        mblnchanged = True
    End Sub

    Private Sub lstROSCVS_ItemCheck(ByVal sender As System.Object, ByVal e As DevExpress.XtraEditors.Controls.ItemCheckEventArgs) Handles lstROSCVS.ItemCheck
        mblnchanged = True
    End Sub

    Private Sub lstROSCNS_ItemCheck(ByVal sender As System.Object, ByVal e As DevExpress.XtraEditors.Controls.ItemCheckEventArgs) Handles lstROSCNS.ItemCheck
        mblnchanged = True
    End Sub
End Class
